////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CHubState.h
//
//		Author			:		Kevin Jordan
//
//		Purpose			:		A header for all functions and members  the Hub State
//
//		Dates			:		Written( March 2, 2011 ) by Kevin Jordan		LastUpdate( March 2, 2011  ) by Kevin Jordan 
//	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CHubState.h"
#include "CMainMenuState.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CSaveOrLoadState.h"
#include "CGamePlayState.h"
#include "CGame.h"
#include "CTestingLevelState.h"
#include "VIN_ObjectManager.h"
#include "VIN_BitMapFont.h"
#include "VIN_SoundManager.h"
#include "CHighScoreState.h"


CHubState::CHubState()
{
	m_nLevelSelected = 0;
	m_nShopSelection = 0;
	m_bInShop = false;
	ImageID = -1;
	PlayerIsInit = false;
	m_bExitingToMain = false;
	pPlayer = NULL;
}

CHubState::~CHubState()
{

}

void CHubState::Enter()
{
	//PlayerImage = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/JackieChanStand.png");
	ImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/0.jpg");
	CursorID = CMainMenuState::GetInstance()->GetCursorID();
	VIN_SoundManager::GetInstance()->PlayBGM(BGM_OTHERMENUS);

	if( !PlayerIsInit )
	{
		//Instantiate a new Player

		pPlayer = new CPlayer();
		pPlayer->SetHeight(96);
		pPlayer->SetWidth(48);
		pPlayer->SetPosX(400);
		pPlayer->SetPosY(300);
		pPlayer->SetType(OBJ_PLAYER);
		pPlayer->SetImageID(PlayerImage);
		pPlayer->SetCash(00);
		VIN_ObjectManager::GetInstance()->SetPlayer(pPlayer);
		PlayerIsInit = true;
	}
	else
	{
		// VIN_ObjectManager::GetInstance()->AddObject(1,pPlayer);
	}
	char buffer[100];
	int fileNumber = 1;

	int x = 0;

	for( int i = 0; i < 8; i++)
	{
		sprintf_s(buffer, sizeof(buffer), "./resource/Graphics/Shop_Pic%d.png", fileNumber );
		m_nShopImage[i] = CSGD_TextureManager::GetInstance()->LoadTexture(buffer);
		fileNumber++;
	}
}

void CHubState::Exit()
{
	m_nShopSelection = 0;
	m_nSelection = 0;
	if( m_bExitingToMain )
	{
		PlayerIsInit = false;
	}

}

void CHubState::Update( float fElapsedTime )
{


}

void CHubState::Render()
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();

	pD3D->Clear(128,78,33);
	pTM->Draw(ImageID, 0,0, 1.75f, 1.25f);

	RECT MenuRect;
	SetRect( &MenuRect, 300, 100, 600, 350 );

	RECT MenuRectII;
	SetRect( &MenuRectII, 285, 85, 615, 365 );

	pD3D->GetSprite()->Flush();
	pD3D->DrawRect( MenuRectII, 255,255,255);
	pD3D->DrawRect( MenuRect, 0,0,0);

	if( !m_bInShop )
	{

		pD3D->GetSprite()->Flush();

		pBF->Render("Shop", 350,150,.75f, D3DCOLOR_ARGB(255,255,0,0),MENUFONT);
		pBF->Render("Play", 350,200,.75f, D3DCOLOR_ARGB(255,255,0,0),MENUFONT);
		pBF->Render("Main Menu", 350,250,.75f, D3DCOLOR_ARGB(255,255,0,0),MENUFONT);
		pBF->Render("Save and Load", 350,300,.75f, D3DCOLOR_ARGB(255,255,0,0),MENUFONT);

		switch( m_nSelection )
		{
		case 0:
			{
				pTM->Draw(CursorID, 320,150);
			}
			break;
		case 1:
			{
				pTM->Draw(CursorID, 320,200);
			}
			break;
		case 2:
			{
				pTM->Draw(CursorID, 320,250);
			}
			break;
		case 3:
			{
				pTM->Draw(CursorID, 320,300);
			}
			break;
		}
	}
	else if( m_bInShop )
	{
		switch( m_nShopSelection )
		{
		case 0:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );																	
			}
			break;
		case 1:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );

			}
			break;
		case 2:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 3:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 4:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 5:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 6:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );
			}
			break;
		case 7:
			{
				pTM->Draw(m_nShopImage[m_nShopSelection], 0,0, 1.25f, 1.25f );    
			}
			break;

		}
		pD3D->GetSprite()->Flush();
		char bufferCombat[100];
		sprintf_s(bufferCombat, sizeof(bufferCombat), "Combat : %d", CHubState::GetInstance()->GetPlayer()->GetCombat() );

		char bufferDurability[100];
		sprintf_s(bufferDurability, sizeof(bufferDurability), "Durability : %d", CHubState::GetInstance()->GetPlayer()->GetDurability() );

		char bufferSpeed[100];
		sprintf_s(bufferSpeed, sizeof(bufferSpeed), "Speed: %d", CHubState::GetInstance()->GetPlayer()->GetSpeed() );

		char bufferRage[100];
		sprintf_s(bufferRage, sizeof(bufferRage), "Rage: %d", CHubState::GetInstance()->GetPlayer()->GetRage() );

		char bufferCash[100];
		sprintf_s(bufferCash, sizeof(bufferCash), "Cash: $%d", CHubState::GetInstance()->GetPlayer()->GetCash() );

		char bufferCostToNext[100];

		//Combat
		pBF->Render(bufferCombat, 100, 206, 0.50f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		if( CHubState::GetInstance()->GetPlayer()->GetCombat() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 75, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}
		else if( CHubState::GetInstance()->GetPlayer()->GetCombat()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetCombat()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", ( ( CHubState::GetInstance()->GetPlayer()->GetCombat() + 1) * 300));
			pBF->Render(bufferCostToNext, 75, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}
		else
		{
			pBF->Render("MAX", 100, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}

		//Durability
		pBF->Render(bufferDurability, 275, 206, 0.50f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		if( CHubState::GetInstance()->GetPlayer()->GetDurability() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 250, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		}
		else if( CHubState::GetInstance()->GetPlayer()->GetDurability()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetDurability()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", (  ( CHubState::GetInstance()->GetPlayer()->GetDurability() + 1) * 300));
			pBF->Render(bufferCostToNext, 250, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);				
		}
		else
		{		
			pBF->Render("MAX", 275, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}

		//Speed
		pBF->Render(bufferSpeed, 475, 206, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		if( CHubState::GetInstance()->GetPlayer()->GetSpeed() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 450, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		}
		else if( CHubState::GetInstance()->GetPlayer()->GetSpeed()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetSpeed()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", (  ( CHubState::GetInstance()->GetPlayer()->GetSpeed() + 1) * 300));
			pBF->Render(bufferCostToNext, 450, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);										
		}
		else
		{
			pBF->Render("MAX", 475, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);	
		}

		//Rage
		pBF->Render(bufferRage, 675, 206, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						
		if( CHubState::GetInstance()->GetPlayer()->GetRage() == 0 )
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", 300);
			pBF->Render(bufferCostToNext, 650, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);						

		}
		else if( CHubState::GetInstance()->GetPlayer()->GetRage()  > 0 &&  CHubState::GetInstance()->GetPlayer()->GetRage()  < 4)
		{
			sprintf_s(bufferCostToNext, sizeof(bufferCostToNext), "Cost: $%d", (  ( CHubState::GetInstance()->GetPlayer()->GetRage() + 1) * 300));
			pBF->Render(bufferCostToNext, 650, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);										
		}
		else
		{				
			pBF->Render("MAX", 675, 256, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
		}

		//Cash display
		pBF->Render(bufferCash, 675, 300, 0.40f, D3DCOLOR_ARGB(255, 255, 0, 0), GAMEFONT);
	}
}


CHubState* CHubState::GetInstance()
{
	static CHubState instance;
	return &instance;
}

bool CHubState::Input()
{
	CSGD_DirectInput*	pDI = CSGD_DirectInput::GetInstance();
	CGame*				pGameState = CGame::GetInstance();

	if( !m_bInShop )
	{
		if( pDI->KeyPressed( DIK_UP ) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP) ||
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_UP))
		{
			VIN_SoundManager::GetInstance()->PlayMenuSelect();
			m_nSelection--;
			if( m_nSelection < 0 )
				m_nSelection = 3;
		}

		if( pDI->KeyPressed( DIK_DOWN ) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_DOWN))
		{
			VIN_SoundManager::GetInstance()->PlayMenuSelect();

			m_nSelection++;
			if( m_nSelection > 3 )
				m_nSelection = 0;
		}
		if( pDI->KeyPressed( DIK_ESCAPE ))
					pGameState->ChangeState( CMainMenuState::GetInstance());
	
		if( pDI->KeyPressed( DIK_RETURN ) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1) )
		{
			VIN_SoundManager::GetInstance()->PlayMenuConfirm();

			switch( m_nSelection )
			{
			case 0:
				{
					//Change the State to the Shop
					m_bInShop = true;
				}
				break;

			case 1:
				{
					//Change to the TestingLevel
					m_nLevelSelected = 0;
					CSGD_DirectInput::GetInstance()->ClearInput();
					pGameState->ChangeState( CGamePlayState::GetInstance());

				}
				break;

			case 2:
				{
					//MainMenu
					m_bExitingToMain = true;
					pGameState->ChangeState( CMainMenuState::GetInstance() );

				}
				break;
			case 3:
				{
					//Save and Load States
					CGamePlayState::GetInstance()->UpdatePlayerProfile();
					pGameState->ChangeState( CSaveOrLoadState::GetInstance() );

				}
				break;

			}
		}
	}
	else if( m_bInShop )
	{
		if( pDI->KeyPressed( DIK_LEFT ) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_LEFT))
		{
			m_nShopSelection--;
			if( m_nShopSelection < 0 )
				m_nShopSelection = 7;
		}

		if( pDI->KeyPressed( DIK_RIGHT )|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_RIGHT) )
		{
			m_nShopSelection++;
			if( m_nShopSelection > 7 )
				m_nShopSelection = 0;
		}

		if( pDI->KeyPressed( DIK_DOWN )|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_DOWN) )
		{
			m_nShopSelection+= 4;
			if( m_nShopSelection > 7 )
				m_nShopSelection -= 8;
		}

		if( pDI->KeyPressed( DIK_UP ) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP) || 
			CSGD_DirectInput::GetInstance()->JoystickDPadPressed(DIR_UP)  )
		{
			m_nShopSelection -= 4;
			if( m_nShopSelection < 0 )
				m_nShopSelection += 8;
		}

		if( pDI->KeyPressed(DIK_ESCAPE) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(3))
		{
			m_bInShop = false;
			m_nShopSelection = 0;
		}

		if( pDI->KeyPressed( DIK_RETURN ) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(1) )
		{
			switch( m_nShopSelection )
			{
			case 0:
				{
					if( GetPlayer()->GetCash() >= (GetPlayer()->GetCombat()+1) * 300 && GetPlayer()->GetCombat() <= 3 )
					{

						GetPlayer()->SetCombat( GetPlayer()->GetCombat() + 1 );
						GetPlayer()->SetCash( GetPlayer()->GetCash() - ( GetPlayer()->GetCombat() * 300 ) );
					}
				}
				break;
			case 1:
				{
					if( GetPlayer()->GetCash() >= (GetPlayer()->GetDurability()+1) * 300 && GetPlayer()->GetDurability() <= 3 )
					{

						GetPlayer()->SetDurability( GetPlayer()->GetDurability() + 1 );
						GetPlayer()->SetCash( GetPlayer()->GetCash() - ( GetPlayer()->GetDurability() * 300 ) );

					}

				}
				break;
			case 2:
				{
					if( GetPlayer()->GetCash() >= (GetPlayer()->GetSpeed()+1) * 300 && GetPlayer()->GetSpeed() <= 3 )
					{
						GetPlayer()->SetSpeed( GetPlayer()->GetSpeed() + 1 );  
						GetPlayer()->SetCash( GetPlayer()->GetCash() - ( GetPlayer()->GetSpeed() * 300 ) );
					}
				}
				break;
			case 3:
				{
					if( GetPlayer()->GetCash() >= (GetPlayer()->GetRage()+1) * 300 && GetPlayer()->GetRage() <= 3 )
					{
						GetPlayer()->SetRage( GetPlayer()->GetRage() + 1 );
						GetPlayer()->SetCash( GetPlayer()->GetCash() - ( GetPlayer()->GetRage() * 300 ) );
					}
				}
				break;
			case 4:
				{

				}
				break;
			case 5:
				{


				}
				break;
			case 6:
				{

				}
				break;
			case 7:
				{


				}
				break;
			}
		}
	}
	return true;
}